// pages/ownerOertification/ownerOertification.js
var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes();
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
// var QRCode = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:applet_park_api_url_imgUrl,
    restaurantname: '',
    titenname: '常用餐厅',
    currenttDate: '',
    getcurrenttDate: '',
    val: '',
    isScheduled: false,
    orderType: '1',
    scheduledTime: '',
    multiIndex: [0, 0, 0],
    restaurantId: '',
    lists: [],
    currenttab: '0',
    selected: 'CHN',
    showModal: false,
    comshowModal: false,
    condition: false,
    listarea: ['请选择性别', 'A区', 'B区', 'C区', 'D区'],
    listareaid: [null, 'personal_use', 'business_use', 'b2p_use', 'rental_use'],
    listareaindex: 0,
    Positive: '',
    unPositive: '',
    title: '',
    radioitems: [{
      name: 'CHN',
      value: '中国',
      checked: 'true'
    }, ],
    radioitems2: [{
      name: '22',
      value: '2112',
    }, ],

    multiArray: [
      ['今天', '明天', '3-2', '3-3', '3-4', '3-5'],
      [0, 1, 2, 3, 4, 5, 6],
      [0, 10, 20]
    ],
  },
  radioChange: function(e) {

    
    if (e.detail.value == '22') {//预约
      this.open()
      this.setData({
        showModal: false,
        isScheduled: true,
      })
    } else {//店内就餐
      this.setData({
        scheduledTime: '',
        isScheduled: false,
      })
    }
    this.setData({
      selected: e.detail.value
    })
  },
  packBp(e) {
    this.setData({
      currenttab: e.currentTarget.dataset.index
    })
  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  cancel: function() {
    this.open();
    this.setData({
      showModal: true
    })
    //省市数组还原
    this.data.provinces = this.data.previousProvinces;
    this.data.citys = this.data.previousCitys;
    this.data.value = this.data.previousValue;
    this.setData(this.data);
  },
  open: function() {
    this.setData({
      condition: !this.data.condition
    });
    this.setData({
      showModal: true
    })
  },
  go: function() {
    this.setData({
      showModal: false,
      comshowModal: false
    })
  },
  //选择预约时间
  bindChange: function(e) {
    var val = e.detail.value;
    
    date = new Date();

    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];

    if (monthDay === "今天") {
      var month = date.getMonth() + 1;
      var day = date.getDate();
      monthDay = this.data.currenttDate;

      currentHours = date.getHours();
      currentMinute = date.getMinutes();
      that.loadHoursMinute(currentHours, currentMinute)

    } else if (monthDay === "明天") {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = that.getBefOrAft(1, this.data.currenttDate);
      that.loadHoursMinute(0, 0)
      that.setData(that.data)

    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      monthDay = month + "月" + day + "日";
    }



    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];
    var startDate = monthDay + " " + hours + ":" + minute + ":00";
    
    that.setData({
      scheduledTime: startDate,
      multiIndex: val
    })
    //最终预约时间

  },
  confirmCity: function() {
    var that = this;
    that.open();
    var val = that.data.multiIndex;
    
    date = new Date();

    var that = this;
    var monthDay = that.data.multiArray[0][val[0]];
    if (monthDay === "今天") {

      monthDay = this.data.currenttDate;


    } else if (monthDay === "明天") {

      monthDay = that.getBefOrAft(1, this.data.currenttDate);


    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      monthDay = month + "月" + day + "日";
    }




    var hours = that.data.multiArray[1][val[1]];
    var minute = that.data.multiArray[2][val[2]];
    var startDate = monthDay + " " + hours + ":" + minute + ":00";
    
    that.setData({
      scheduledTime: startDate,
      multiIndex: val
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadData();
    this.pickerTap();
    this.getcurrenttDate();
  },
  //选择
  changeUsedType: function(e) {
    
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  selectrest(e) {
    this.setData({
      restaurantId: e.currentTarget.dataset.restaurantid,
      restaurantname: e.currentTarget.dataset.restaurantname,
      comshowModal: true,
      showModal: true
    })


  },

  loadData() {
    var that = this;

    requestDataparams('/api/restaurants', {}, 'GET').then(res => {
      var lists = res.data.data;
      that.setData({
        lists: lists
      })
    }).catch(err => {
      
    });
    // wx.request({
    //   url: applet_park_api_url + '/api/restaurants',
    //   method: 'GET',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function(res) {
    //     )

    //     if (res.data.code == 0) {
    //       var lists = res.data.data;

    //       that.setData({
    //         lists: lists
    //       })

    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     )
    //   }

    // })
  },

  serch(e) {
    var that = this;
    // let keyword=e.
    that.setData({
      lists: []
    })

    if (e.detail.value != '') {
      that.setData({
        titenname: '搜索结果'
      })

    }

    // wx.request({
    //   url: applet_park_api_url + '/api/restaurants',
    //   method: 'GET',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   data: {
    //     name: e.detail.value
    //   },
    //   success: function(res) {
    //     )

    //     if (res.data.code == 0) {
    //       var lists = res.data.data;
    //       that.setData({
    //         lists: lists
    //       })
    //       if (lists.length == 0) {
    //         that.setData({
    //           title: "暂无内容"
    //         })
    //       } else {
    //         that.setData({
    //           title: ""
    //         })
    //       }

    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     )
    //   }

    // })

    requestDataparams('/api/restaurants', { name: e.detail.value}, 'GET').then(res => {
      if (res.data.code == 0) {
        var lists = res.data.data;
        that.setData({
          lists: lists
        })
        if (lists.length == 0) {
          that.setData({
            title: "暂无内容"
          })
        } else {
          that.setData({
            title: ""
          })
        }

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      
    });
  },
  torestLIst(e) {
    
    //食堂id
    //是否预约
    //预约时间
    wx.navigateTo({
      url: '../restaurantHomepage/restaurantHomepage?restaurantId=' + this.data.restaurantId + '&isScheduled=' + this.data.isScheduled + '&scheduledTime=' + this.data.scheduledTime + '&orderType=' + e.currentTarget.dataset.ordertype,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      showModal: false,
      comshowModal: false,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
     this.setData({
       showModal: false,
       condition: false,
     })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 预约时间自定义格式化
  pickerTap: function() {
    date = new Date();

    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];

    currentHours = date.getHours();
    currentMinute = date.getMinutes();

    // 月-日
    // for (var i = 2; i <= 28; i++) {
    //   var date1 = new Date(date);
    //   date1.setDate(date.getDate() + i);
    //   var md = (date1.getMonth() + 1) + "-" + date1.getDate();
    //   monthDay.push(md);
    // }

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };

    // if (data.multiIndex[0] === 0) {
    //   if (data.multiIndex[1] === 0) {
    //     this.loadDatadata(hours, minute);
    //   } else {
    //     this.loadMinute(hours, minute);
    //   }
    // } else {

    // }
    this.loadHoursMinute(currentHours, currentMinute)
    data.multiArray[0] = monthDay;
    // data.multiArray[1] = hours;
    // data.multiArray[2] = minute;

    this.setData(data);

  },
  loadDatadata: function(hours, minute) {

    var minuteIndex = 60;
    // if (currentMinute > 0 && currentMinute <= 10) {
    //   minuteIndex = 10;
    // } else if (currentMinute > 10 && currentMinute <= 20) {
    //   minuteIndex = 20;
    // } else if (currentMinute > 20 && currentMinute <= 30) {
    //   minuteIndex = 30;
    // } else if (currentMinute > 30 && currentMinute <= 40) {
    //   minuteIndex = 40;
    // } else if (currentMinute > 40 && currentMinute <= 50) {
    //   minuteIndex = 50;
    // } else {
    //   minuteIndex = 60;
    // }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = 0; i < 60; i += 5) {
        minute.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = minuteIndex; i < 60; i += 5) {
        minute.push(i);
      }
    }
  },

  loadHoursMinute: function(hours, minute) {
    // 时
    let hours1 = [];
    var minute1 = [];
    for (var i = hours; i < 24; i++) {
      hours1.push(i);
    }
    // 分
    for (var i = minute; i < 60; i += 5) {
      minute1.push(i);
    }

    this.data.multiArray[1] = hours1;
    this.data.multiArray[2] = minute1;
    this.setData(this.data)
  },

  loadMinute: function(hours, minute) {
    var minuteIndex = 60;
    // if (currentMinute > 0 && currentMinute <= 10) {
    //   minuteIndex = 10;
    // } else if (currentMinute > 10 && currentMinute <= 20) {
    //   minuteIndex = 20;
    // } else if (currentMinute > 20 && currentMinute <= 30) {
    //   minuteIndex = 30;
    // } else if (currentMinute > 30 && currentMinute <= 40) {
    //   minuteIndex = 40;
    // } else if (currentMinute > 40 && currentMinute <= 50) {
    //   minuteIndex = 50;
    // } else {

    // }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  bindStartMultiPickerChange: function(e) {
    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];
    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];

    if (monthDay === "今天") {
      var month = date.getMonth() + 1;
      var day = date.getDate();
      monthDay = that.data.currenttDate;
    } else if (monthDay === "明天") {
      bindChange
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = that.getBefOrAft(1, this.data.currenttDate);

    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      monthDay = month + "月" + day + "日";
    }

    var startDate = monthDay + "-" + hours + "-" + minute;
    that.setData({
      startDate: startDate
    })


  },
  // 获取前天或者后一天  type: 0: 前一天 1：后一天
  getBefOrAft(type, date) {
    let forMatDate = date.replace(/-/g, '/') // 格式化日期 2019-02-23 => 2019/02/23
    let d = new Date(forMatDate) // new Date() 不能识别 2019-01-02  需转化为 2019/01/02
    if (type === 0) {
      d = +d - 1000 * 60 * 60 * 24
    } else if (type === 1) {
      d = +d + 1000 * 60 * 60 * 24 // d前面需要加上 +
    }
    d = new Date(d)
    return this.dateFormat(d) // 格式化 将日期格式转化为我们需要展示格式
  },

  // 格式化日期，传入日期对象   =>2019-01-11  
  dateFormat(date) {
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    let d = date.getDate()
    if (m <= 9) {
      m = '0' + m
    }
    if (d <= 9) {
      d = '0' + d
    }
    return y + '-' + m + '-' + d
  },
  getcurrenttDate() {
    var date = new Date()
    var year = date.getFullYear()
    var month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
    var day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    this.data.currenttDate = year + '-' + month + '-' + day;
  }

})